define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'asking_back/index' + location.search,
                    add_url: 'asking_back/add',
                    edit_url: 'asking_back/edit',
                    del_url: 'asking_back/del',
                    multi_url: 'asking_back/multi',
                    table: 'asking_back',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'user.username', title: __('User.username')},
                        {field: 'asking.title', title: __('Asking.title')},
                        {field: 'content', title: __('Content')},
                        {field: 'is_true', title: __('Is_true'), searchList: {"0":__('Is_true 0'),"1":__('Is_true 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        asking_back: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'asking_back/asking_back' + location.search,
                    add_url: 'asking_back/add?asking_id='+Fast.api.query("asking_id"),
                    edit_url: 'asking_back/edit?asking_id='+Fast.api.query("asking_id"),
                    del_url: 'asking_back/del',
                    multi_url: 'asking_back/multi',
                    table: 'asking_back',
                },
                queryParams: function (params) { //自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {};  //并将搜索过滤器 转为对象方便我们追加条件
                    filter.asking_id= Fast.api.query("asking_id");     //将需要的参数asking_id 加入到搜索条件中去 要求url传递的参数必须为group_id=XX;可以用 Fast.api.query("group_id")获取到！
                    op.asking_id= "=";  //asking_id的操作方法的为 找到相等的
                    params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                    params.op = JSON.stringify(op);
                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({url: 'login'});
                    //params.op = JSON.stringify({url: 'like'});
                    return params;
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'user.username', title: __('User.username')},
                        {field: 'asking.title', title: __('Asking.title'), visible: false},
                        {field: 'content', title: __('Content')},
                        {field: 'is_true', title: __('Is_true'), searchList: {"0":__('Is_true 0'),"1":__('Is_true 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.toggle},
                        {
                            field: 'ajax',
                            width: "120px",
                            title: "设置靠谱",
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: function (row) {
                                        if(row.is_true==1){
                                            return '取消';
                                        }
                                        if(row.is_true==1){
                                            return '设置';
                                        }

                                    },
                                    title: function (row) {
                                        if(row.is_true==1){
                                            return '取消';
                                        }
                                        if(row.is_true==1){
                                            return '设置';
                                        }
                                    },
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: function (row) {
                                        return 'asking_back/set_goods?asking_id='+row.id+'&ids='+row.id+'&is_true='+row.is_true;
                                    },
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        if(ret.code==1){
                                            $(".btn-refresh").trigger('click');
                                        }
                                        //如果需要阻止成功提示，则必须使用return false;
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});